package com.group2.view;

import java.awt.Font;
import java.io.*;
import java.net.Socket;
import model.*;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

//import com.group2.dao.CourseDao;
import model.Course;
import com.group2.util.DbUtil;
import com.group2.util.StringUtil;
import model.*;

import static model.netBase.IP;
import static model.netBase.IP;
/*选课系统模块
 * 管理员添加课程信息*/
public class CourseAddFrame extends javax.swing.JFrame {

	private JTextField courseIdTxt;
	private JTextField capacityTxt;
	private JTextField courseNameTxt;
	private JTextField courseTeacherTxt;
	private JComboBox<String> courseTimeTxt_week;//下拉框设置课程周几
	private JTextField courseTimeTxt_Begin;
	private JTextField courseTimeTxt_End;
	private JTextField courseLocationTxt;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JLabel jLabel6;
	private JLabel jLabel7;
	private JLabel jLabel8;
	private JButton jb_add;
	private JButton jb_reset;
	private JPanel contentPane;
	private boolean f=false;

	DbUtil dbUtil = new DbUtil();
	//CourseDao coursedao = new CourseDao();

	public CourseAddFrame() {
		initComponents();
		Font font = new Font("Dialog", Font.PLAIN, 12);
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, font);
			}
		}
		initComponents();
		setBounds(100, 200, 500, 300);
		this.setLocationRelativeTo(null);
	}
	


	private void initComponents() {

		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jLabel4 = new JLabel();
		jLabel5 = new JLabel();
		jLabel6 = new JLabel();
		jLabel7 = new JLabel();
		jLabel8 = new JLabel();
		
		courseTimeTxt_Begin = new JTextField();
		courseTimeTxt_End = new JTextField();
		courseNameTxt = new JTextField();
		courseTeacherTxt = new JTextField();
		capacityTxt = new JTextField();
		courseLocationTxt = new JTextField();
		courseIdTxt=new JTextField();
		
		String[]Weekdays={"周一","周二","周三","周四","周五","周六","周日"};
		courseTimeTxt_week=new JComboBox<>();
		courseTimeTxt_week.addItem("周一");
		courseTimeTxt_week.addItem("周二");
		courseTimeTxt_week.addItem("周三");
		courseTimeTxt_week.addItem("周四");
		courseTimeTxt_week.addItem("周五");
		courseTimeTxt_week.addItem("周六");
		courseTimeTxt_week.addItem("周日");
		
		jb_add = new JButton();
		jb_reset = new JButton();

		//setClosable(true);
		//setIconifiable(true);
		setTitle("添加课程信息");

		jLabel1.setText("课程名称：");
		jLabel2.setText("课程编号：");
		jLabel3.setText("任课教师：");
		jLabel4.setText("课程容量：");
		jLabel5.setText("上课地点：");
		jLabel6.setText("上课时间：");
		jLabel7.setText("开始时间：");
		jLabel8.setText("结束时间：");
		
		courseTimeTxt_Begin=new JTextField();
		courseTimeTxt_End = new JTextField();
		courseNameTxt = new JTextField();
		courseTeacherTxt = new JTextField();
		capacityTxt = new JTextField();
		courseLocationTxt = new JTextField();
		courseIdTxt=new JTextField();
		
		contentPane=new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		jLabel1.setBounds(20,20,70,30);courseNameTxt.setBounds(100,20,100,30);
		jLabel2.setBounds(20,70,70,30);courseIdTxt.setBounds(100,70,100,30);
		jLabel3.setBounds(20,120,70,30);courseTeacherTxt.setBounds(100,120,100,30);
		jLabel4.setBounds(20,170,70,30);capacityTxt.setBounds(100,170,100,30);
		
		jLabel5.setBounds(270,20,70,30);courseLocationTxt.setBounds(350,20,80,30);
		jLabel6.setBounds(270,70,70,30);courseTimeTxt_week.setBounds(350,70,80,30);
		jLabel7.setBounds(270,120,70,30);courseTimeTxt_Begin.setBounds(350,120,80,30);
		jLabel8.setBounds(270,170,70,30);courseTimeTxt_End.setBounds(350,170,80,30);
		
		contentPane.add(jLabel1);contentPane.add(courseNameTxt);
		contentPane.add(jLabel2);contentPane.add(courseIdTxt);
		contentPane.add(jLabel3);contentPane.add(courseTeacherTxt);
		contentPane.add(jLabel4);contentPane.add(capacityTxt);
		contentPane.add(jLabel5);contentPane.add(courseLocationTxt);
		contentPane.add(jLabel6);contentPane.add(courseTimeTxt_week);
		contentPane.add(jLabel7);contentPane.add(courseTimeTxt_Begin);
		contentPane.add(jLabel8);contentPane.add(courseTimeTxt_End);

		jb_add.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/add.png")));
		jb_add.setText("添加");
		jb_add.setBounds(140,215,80,27);
		jb_add.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					jb_addActionPerformed(evt);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		});
		contentPane.add(jb_add);

		jb_reset.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/reset.png")));
		jb_reset.setText("重置");
		jb_reset.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_resetActionPerformed(evt);
			}
		});
		jb_reset.setBounds(260,215,80,27);
		contentPane.add(jb_reset);
	}

	private void jb_addActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
		String courseName = this.courseNameTxt.getText();
		String courseLocation = this.courseLocationTxt.getText();
		String courseTeacher = this.courseTeacherTxt.getText();
		String courseCapacity = this.capacityTxt.getText();
		String courseId=this.courseIdTxt.getText();
		String cW=(String) this.courseTimeTxt_week.getSelectedItem();
		String cB=this.courseTimeTxt_Begin.getText();
		String cE=this.courseTimeTxt_End.getText(); 
		
		
		if (StringUtil.isEmpty(courseName)) {
			JOptionPane.showMessageDialog(this, "课程名称不能为空!");
			return;
		}
		if (StringUtil.isEmpty(courseTeacher)) {
			JOptionPane.showMessageDialog(this, "任课老师不能为空!");
			return;
		}
		if (StringUtil.isEmpty(courseCapacity)) {
			JOptionPane.showMessageDialog(this, "课程容量不能为空!");
			return;
		}
		Course course=new Course(courseId, courseName, cW,Integer.parseInt(cB),Integer.parseInt(cE),
				courseTeacher, courseLocation,Integer.parseInt(courseCapacity)) ;
		System.out.println("开始传输");
		//拉取选课队列
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		InputStream is = socket.getInputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		//传输请求业务+学生id
		System.out.println(courseId);
		bw.write("49" + '\n'+courseId+'\n'+courseName+'\n'+cW+'\n'+cB+'\n'+cE+'\n'+courseTeacher+'\n'+courseLocation+'\n'+courseCapacity+'\n');
		bw.flush();
		//从服务端输入到本地
		String str= br.readLine();
		System.out.println(str);
		System.out.println("传输结束");
		//输出输入流关闭
		os.close();
		socket.close();
		if (str.equals("true")) {
			JOptionPane.showMessageDialog(this, "添加成功!");
			this.dispose();
			f=true;
		} else {
			JOptionPane.showMessageDialog(this, "添加失败!");
		}
	}
	public boolean get_f()
	{
		return f;
	}




	private void jb_resetActionPerformed(java.awt.event.ActionEvent evt) {
		this.resetValue();
	}

	private void resetValue() {
		this.courseIdTxt.setText("");
		this.courseLocationTxt.setText("");
		this.courseNameTxt.setText("");
		this.courseTeacherTxt.setText("");
		this.courseTimeTxt_Begin.setText("");
		this.courseTimeTxt_End.setText("");
		this.capacityTxt.setText("");
	}
	
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CourseAddFrame().setVisible(true);
			}
		});
	}

}
